﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Problema2
{
    class DiskDatabase : CustomDatabase
    {
        private StreamWriter sw;
        private string File;

        public DiskDatabase()
        {
            Console.WriteLine("Nume fisier pentru baza de date:");
            File = Console.ReadLine();
            sw = new StreamWriter(File);
        }

        public override void storeCustomer(Customer c)
        {
            Customer nou = c.Clone();
            sw.WriteLine(nou.Nume + "\t" + nou.PC.ToString());
        }

        public override void afisare()
        {
            Console.WriteLine("Disk Database: check {0} file",File);
            sw.Close();
        }
        public override void sterge()
        {
            Console.WriteLine("Introduceti numele clientului pe care doriti sa-l stergeti:");
            string nume = Console.ReadLine();
            StreamReader sr = new StreamReader(File);
            string[] linii = new string[100];
            int i = 0;
            while (sr.EndOfStream == false)
            {
                string line = sr.ReadLine();
                if (line.StartsWith(nume) == false)
                {
                    linii[i] = line;
                    i++;
                }
            }
            sr.Close();
            sw = new StreamWriter(File);
            for (int j = 0; j < i; j++)
                sw.WriteLine(linii[j]);
            sw.Close();
        }
    }
}
